//
//  ConReportAlreadyController.m
//  Shengzhuli
//
//  Created by FXGuo on 2017/8/9.
//  Copyright © 2017年 kamangkeji. All rights reserved.
//

#import "ConReportAlreadyController.h"
#import "AddCustomerCell.h"
#import "ClubNetwork.h"
#import "MBProgressHUD+MJ.h"
#import <MJRefresh.h>

@interface ConReportAlreadyController ()

@property (nonatomic,strong) NSMutableArray *addCustomers;
@property (nonatomic,assign) NSInteger curPage;

@end

@implementation ConReportAlreadyController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"已报名";
    self.curPage = 1;
    [self configNaviColor];
    [self setUpTableView];
    [self getNearAcReportNum];
    [self getNearActivityAddCustomer];
}
- (void)configNaviColor
{
    CGSize imageSize = CGSizeMake(320,64);
    UIGraphicsBeginImageContextWithOptions(imageSize,0, [UIScreen mainScreen].scale);
    [[UIColor whiteColor] set];
    UIRectFill(CGRectMake(0,0, imageSize.width, imageSize.height));
    UIImage *pressedColorImg = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    [self.navigationController.navigationBar setBackgroundImage:pressedColorImg forBarMetrics:UIBarMetricsDefault];
    
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
    
    self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor blackColor],NSFontAttributeName:[UIFont systemFontOfSize:20]};
    
}

- (UIBarButtonItem *)customBackItemWithTarget:(id)target
                                       action:(SEL)action
{
    UIButton *backBtn = [[UIButton alloc]init];
    backBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [backBtn setBackgroundImage:[UIImage imageNamed:@"ic_left_back"] forState:UIControlStateNormal];
    [backBtn setBackgroundImage:[UIImage imageNamed:@"ic_left_back_select"] forState:UIControlStateHighlighted];
    [backBtn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    [backBtn sizeToFit];
    backBtn.contentEdgeInsets = UIEdgeInsetsMake(0, -20, 0, 0);
    return [[UIBarButtonItem alloc]initWithCustomView:backBtn];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.addCustomers.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    AddCustomerCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([AddCustomerCell class])];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = self.addCustomers[indexPath.row];
    return cell;
}
- (void)setUpTableView
{
    self.tableView.tableFooterView = [[UIView alloc]init];
    self.tableView.estimatedRowHeight = 130;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
//    self.tableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([AddCustomerCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([AddCustomerCell class])];
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        self.curPage++;
        [self getNearActivityAddCustomer];
    }];
}
- (void)getNearActivityAddCustomer
{
    NSMutableDictionary *parame = [NSMutableDictionary dictionary];
    parame[@"pageNo"] = @(self.curPage);
    parame[@"activityId"] = @(self.ID);
    [self showClearBgWaiter];
    [ClubNetwork getNearAdderListWithParame:parame response:^(BOOL success, NSString *msg,NSArray *list) {
        [self hideWaiter];
        if (success) {
            self.tableView.mj_footer.hidden = list.count<20;
            if (self.curPage == 1&&list.count == 0) {
                [self showNoDataLab:self.tableView range:CGRectMake(0, ScreenH*0.5-60, ScreenW, 20) title:@"暂无数据"];
                return ;
            }
            [self.addCustomers addObjectsFromArray:list];
            [self.tableView reloadData];
        }else {
            [MBProgressHUD showError:msg];
        }
    }];
}
- (void)getNearAcReportNum
{
    NSMutableDictionary *parame = [NSMutableDictionary dictionary];
    parame[@"activityId"] = @(self.ID);
    [self showClearBgWaiter];
    [ClubNetwork getNearAcNumWithParame:parame response:^(BOOL success, NSString *msg,NSInteger num) {
        [self hideWaiter];
        if (success) {
            self.title = [NSString stringWithFormat:@"已报名(%zd)",num];
        }else {
            [MBProgressHUD showError:msg];
        }
    }];
}
- (NSMutableArray *)addCustomers
{
    if (!_addCustomers) {
        _addCustomers = [NSMutableArray array];
    }
    return _addCustomers;
}
@end
